﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;

namespace TranTekBE
{
    public partial class Product : PagePopUp
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Page.IsPostBack == false)
            {
                this.LoadCategory();
            }
        }

        private void LoadCategory()
        {
            try
            {
                DataSet ds = FunctionsUtils.Database.ExecuteQuery("Select CategoryISN, catName From Category Where catStatus = 1");

                lbCategoryLeft.DataSource = ds.Tables[0];
                lbCategoryLeft.DataTextField = "catName";
                lbCategoryLeft.DataValueField = "CategoryISN";
                lbCategoryLeft.DataBind();
            }
            catch(Exception ex)
            {
                ltError.Text = ex.Message;
                log.WriteLog("Error in Add Product: {0} {1}", ex, ex.StackTrace);
            }
        }

        protected void ControlClick_Command(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Submit":
                    if (this.CheckData() == true)
                    {
                        this.SaveAddNewProduct(txtProductName.Text.Trim(), FunctionsUtils.ConvertToInt(rblStatus.SelectedValue, 0), FunctionsUtils.ConvertToDecimal(txtPrice.Text.Trim(), 0), FunctionsUtils.ConvertToDecimal(txtOnhandQty.Text.Trim(), 0), FunctionsUtils.ConvertFromListBoxToStringList(lbCategoryRight), txtComments.Text.Trim(), 0);
                    }
                    break;
                case "MoveRight":
                    FunctionsUtils.AddItemsToAnotherListBoxAndDeleteIt(lbCategoryLeft, lbCategoryRight, false);
                    break;
                case "MoveLeft":
                    FunctionsUtils.AddItemsToAnotherListBoxAndDeleteIt(lbCategoryRight, lbCategoryLeft, false);
                    break;
                case "MoveAllRight":
                    FunctionsUtils.AddItemsToAnotherListBoxAndDeleteIt(lbCategoryLeft, lbCategoryRight, true);
                    break;
                case "MoveAllLeft":
                    FunctionsUtils.AddItemsToAnotherListBoxAndDeleteIt(lbCategoryRight, lbCategoryLeft, true);
                    break;
            }
        }

        private bool CheckData()
        {
            ltError.Text = string.Empty;
            bool bResult = true;

            if (txtProductName.Text.Trim().Length == 0)
            {
                FunctionsUtils.ShowSmallError(ltProductNameErr, FunctionsUtils.Msg["frm_Required"]);
                bResult = false;
            }

            if (txtPrice.Text.Trim().Length == 0)
            {
                FunctionsUtils.ShowSmallError(ltPriceErr, FunctionsUtils.Msg["frm_Required"]);
                bResult = false;
            }
            else
            {
                if (ValidateUtils.isPosNumber(txtPrice.Text.Trim(), NumberType.Decimal, false) == false)
                {
                    FunctionsUtils.ShowSmallError(ltPriceErr, string.Format(FunctionsUtils.Msg["frm_NumberMin"], "", "0"));
                    bResult = false;
                }
            }

            if (txtOnhandQty.Text.Trim().Length == 0)
            {
                FunctionsUtils.ShowSmallError(ltOnhandQtyErr, FunctionsUtils.Msg["frm_Required"]);
                bResult = false;
            }
            else
            {
                if (ValidateUtils.isNumber(txtOnhandQty.Text.Trim(), NumberType.Integer) == false)
                {
                    FunctionsUtils.ShowSmallError(ltOnhandQtyErr, FunctionsUtils.Msg["frm_Format"]);
                    bResult = false;
                }
            }

            if (lbCategoryRight.Items.Count == 0)
            {
                ltError.Text = string.Format(FunctionsUtils.Msg["sel_Item"], "Category");
                bResult = false;
            }

            return bResult;
        }

        private void SaveAddNewProduct(string proName, int proStatus, decimal proPrice, decimal proOnHandQty, string lstCategoryISN, string proComments, int updateBy)
        {
            try
            {
                int iResult = (int)FunctionsUtils.Database.ExecuteStoredProcReturnObject("xp_productinfo_insupd", 
                    new string[] { "proName", "proStatus", "proPrice", "proOnHandQty", "proComment", "lstCategoryISN", "updatedBy" }, 
                    new object[] { FunctionsUtils.DoubleSingleQuote(proName), proStatus, proPrice, proOnHandQty, FunctionsUtils.DoubleSingleQuote(proComments), lstCategoryISN, updateBy });

                if (iResult < 0)
                {
                    switch (iResult)
                    {
                        case -2: //proName exist
                            ltError.Text = string.Format(FunctionsUtils.Msg["frm_Exist"], "Category");
                            break;
                    }
                }
                else
                {
                    this.ClosePopupScripManager(true);
                }
            }
            catch (Exception ex)
            {
                ltError.Text = ex.Message;
                log.WriteLog("Error in Add Product: {0} {1}", ex, ex.StackTrace);
            }
        }

        //private void AddItemsToAnotherListBoxAndDeleteIt(ListBox lbSource, ListBox lbDestination, bool MoveAll)
        //{
        //    if (MoveAll == false)
        //    {
        //        for (int i = lbSource.GetSelectedIndices().Length - 1; i >= 0; i--)
        //        {
        //            lbDestination.Items.Add(lbSource.Items[i]);
        //            lbSource.Items.RemoveAt(i);
        //        }
        //    }
        //    else
        //    {
        //        for (int i = 0; i < lbSource.Items.Count; i++)
        //        {
        //            lbDestination.Items.Add(lbSource.Items[i]);
        //        }
        //        lbSource.Items.Clear();
        //    }
        //}

        //private string ConvertFromListBoxToStringList(ListBox lb)
        //{
        //    string s = string.Empty;

        //    for (int i = 0; i < lb.Items.Count; i++)
        //    {
        //        s += string.Format("{0},", lb.Items[i].Value);
        //    }
        //    return s.TrimEnd(',');
        //}
    }
}
